package com.book.controller;

import com.book.entity.*;
import com.book.service.AddressService;
import com.book.service.OrderService;
import com.book.util.ResultBack;
import com.book.vo.AddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressService addressService;
    @Resource
    private OrderService orderService;
    
    @RequestMapping("/getAddress")
    @ResponseBody
    public ResultBack getAddress(HttpSession httpSession) {
        Account account = (Account) httpSession.getAttribute("account");
        Long userId = account.getId();
        List<AddressVo> list = addressService.findAddressByUserId(userId.intValue());
        return ResultBack.success("查询地址成功", list);
    }

    @RequestMapping("/changeAddress")
    @ResponseBody
    public ResultBack changeAddress(Long orderId,int addressId) {
        int i = orderService.changeAddressByOrderId(orderId,addressId);
        return ResultBack.success("查询地址成功", i);
    }

    @GetMapping("/findProvinces")
    @ResponseBody
    public ResultBack findProvinces() throws Exception {
        List<Provinces> provincesByCode = addressService.findProvincesByCode();
        return ResultBack.success("查询成功", provincesByCode);
    }

    @GetMapping("/findCities")
    @ResponseBody
    public ResultBack findCities(String provinceCode) throws Exception {
        List<Cities> citiesByprovinceCode = addressService.findCitiesByProvinceCode(provinceCode);
        return ResultBack.success("查询成功", citiesByprovinceCode);
    }

    @GetMapping("/findAreas")
    @ResponseBody
    public ResultBack findAreas(String cityCode) throws Exception {
        List<Areas> areasBycityCode = addressService.findAreasByCityCode(cityCode);
        return ResultBack.success("查询成功", areasBycityCode);
    }

    @GetMapping("/findStreets")
    @ResponseBody
    public ResultBack findStreets(String areaCode) throws Exception {
        List<Streets> streetsByAreaCode = addressService.findStreetsByAreaCode(areaCode);
        return ResultBack.success("查询成功", streetsByAreaCode);
    }

    @GetMapping("/findVillages")
    @ResponseBody
    public ResultBack findVillages(String streetCode) throws Exception {
        List<Villages> villagesByStreetCode = addressService.findVillagesByStreetCode(streetCode);
        return ResultBack.success("查询成功", villagesByStreetCode);
    }




    /**
     * 增加用户收获地址 如果超过5条 则无法继续增加
     */
    @PostMapping("/addAddress")
    @ResponseBody
    public ResultBack addAddress(BookUserAddress bookUserAddress, HttpSession httpSession) throws Exception {
        Account account = (Account) httpSession.getAttribute("account");
        Long userId = account.getId();
        int i = addressService.selectAddressCountByUserId(userId);
        if (i >= 5) {
            return ResultBack.failure( "超过5条地址，新增失败");
        } else {
            bookUserAddress.setAccountId(userId.intValue());
            int result = addressService.addAddressByUserId(bookUserAddress);
            return ResultBack.success("增加成功", result);
        }
    }
}
